package com.sc.nft.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.*;
import com.sc.nft.dto.UserEcologyCastRecordsDao;
import com.sc.nft.dto.UserEcologyHatchRecordsDao;
import com.sc.nft.dto.UserEcologyPointsDao;
import com.sc.nft.entity.ReportUser;
import com.sc.nft.entity.ReportUserDayDetails;
import com.sc.nft.entity.dto.ReportSaveDTO;
import com.sc.nft.entity.vo.ReportUserDayDetailsVO;
import com.sc.nft.entity.vo.ReportUserVO;
import com.sc.nft.enums.DaoLevelTypeEnum;
import com.sc.nft.enums.StatisticalStatementEnum;
import com.sc.nft.helper.ReportOverviewHelper;
import com.sc.nft.service.ReportOverviewService;
import com.sc.nft.service.ReportUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author Sun
 * @since 2023-02-21 17:20:39
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ReportUserServiceImpl implements ReportUserService {
    private final ReportUserDao reportUserDao;
    private final UserInfoDao userInfoDao;
    private final AirdropGrantRecordDao airdropGrantRecordDao;
    private final OrderDao orderDao;
    private final StaticDataDao staticDataDao;
    private final UserSynthesisRecordDao userSynthesisRecordDao;
    private final ReportOverviewService reportOverviewService;
    private final UserEcologyHatchRecordsDao userEcologyHatchRecordsDao;
    private final UserEcologyCastRecordsDao userEcologyCastRecordsDao;
    private final UserEcologyExpenditureRecordDao userEcologyExpenditureRecordDao;
    private final UserEcologyPointsDao userEcologyPointsDao;

    @Override
    public void insertReportUser() {

        ReportUser reportUser = new ReportUser();
        reportUser.setSumPartnerCount(userInfoDao.getPartnerCount());
        reportUser.setSumCityLordCount(userInfoDao.getCountByLevel(DaoLevelTypeEnum.CITY_LORD));
        reportUser.setSumResidentCount(userInfoDao.getCountByLevel(DaoLevelTypeEnum.RESIDENT));
        reportUser.setSumRtouristsCount(userInfoDao.getCountByLevel(DaoLevelTypeEnum.TOURISTS));
        reportUser.setSumUserCount(userInfoDao.getCount());
        reportUser.insert();

        Long residentCollectionId = Long.valueOf(staticDataDao.getByType("resident_collection_id").getValue());
        Long cityLordCollectionId = Long.valueOf(staticDataDao.getByType("city_lord_collection_id").getValue());

        //获取空投商品中的城主数量(昨日)
        Integer cityLordAirdropGrantCount = airdropGrantRecordDao.getYesterDayCountByCollectionId(cityLordCollectionId);
        //获取空投商品中的城主数量(昨日)
        Integer residentAirdropGrantCount = airdropGrantRecordDao.getYesterDayCountByCollectionId(residentCollectionId);

        //昨日售卖的城主卡数量
        Integer cityLordOrderCount = orderDao.getYesterDayCountByCollectionId(cityLordCollectionId);
        //昨日售卖居民卡数量
        Integer residentOrderCount = orderDao.getYesterDayCountByCollectionId(residentCollectionId);

        //昨日合成城主数量
        Integer cityLordSynthesisCount = userSynthesisRecordDao.getYesterDayCountByCollectionId(cityLordCollectionId);
        //昨日合成居民数量
        Integer residentLordSynthesisCount = userSynthesisRecordDao.getYesterDayCountByCollectionId(residentCollectionId);

        //昨日新增人数
        Integer userCount = userInfoDao.getYesterDayCount();

        ReportUserDayDetails reportUserDayDetails = new ReportUserDayDetails();
        reportUserDayDetails.setNewAddCityLordCount(cityLordAirdropGrantCount + cityLordOrderCount + cityLordSynthesisCount);
        reportUserDayDetails.setNewAddResidentCount(residentAirdropGrantCount + residentOrderCount + residentLordSynthesisCount);
        reportUserDayDetails.setNewAddRtouristsCount(userCount);
        reportUserDayDetails.insert();
    }

    @Override
    public void insertReportUserNewEdition() {
        List<ReportSaveDTO> list = new ArrayList<>();

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.NUMBER_OF_LORDS,  Convert.toStr(userInfoDao.getPartnerCount()));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.NUMBER_OF_TOTAL_CITADELS,  Convert.toStr(userInfoDao.getCountByLevel(DaoLevelTypeEnum.CITY_LORD)));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.TOTAL_NUMBER_OF_INHABITANTS,  Convert.toStr(userInfoDao.getCountByLevel(DaoLevelTypeEnum.RESIDENT)));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.TOTAL_NUMBER_OF_VISITORS,  Convert.toStr(userInfoDao.getCountByLevel(DaoLevelTypeEnum.TOURISTS)));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.TOTAL_NUMBER_OF_USERS,  Convert.toStr(userInfoDao.getCount()));


        Long residentCollectionId = Long.valueOf(staticDataDao.getByType("resident_collection_id").getValue());
        Long cityLordCollectionId = Long.valueOf(staticDataDao.getByType("city_lord_collection_id").getValue());

        //获取空投商品中的城主数量(昨日)
        Integer cityLordAirdropGrantCount = airdropGrantRecordDao.getYesterDayCountByCollectionId(cityLordCollectionId);
        //获取空投商品中的城主数量(昨日)
        Integer residentAirdropGrantCount = airdropGrantRecordDao.getYesterDayCountByCollectionId(residentCollectionId);

        //昨日售卖的城主卡数量
        Integer cityLordOrderCount = orderDao.getYesterDayCountByCollectionId(cityLordCollectionId);
        //昨日售卖居民卡数量
        Integer residentOrderCount = orderDao.getYesterDayCountByCollectionId(residentCollectionId);

        //昨日合成城主数量
        Integer cityLordSynthesisCount = userSynthesisRecordDao.getYesterDayCountByCollectionId(cityLordCollectionId);
        //昨日合成居民数量
        Integer residentLordSynthesisCount = userSynthesisRecordDao.getYesterDayCountByCollectionId(residentCollectionId);

        //昨日新增人数
        Integer userCount = userInfoDao.getYesterDayCount();

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.THE_NUMBER_OF_NEW_CITY_MASTERS,  Convert.toStr(cityLordAirdropGrantCount + cityLordOrderCount + cityLordSynthesisCount));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.NEW_RESIDENTS,  Convert.toStr(residentAirdropGrantCount + residentOrderCount + residentLordSynthesisCount));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.NUMBER_OF_NEW_VISITORS,  Convert.toStr(userCount));

        reportOverviewService.saveBath(list);
    }

    @Override
    public void insertReportUserByDay(String day) throws ParseException {

        Long residentCollectionId = Long.valueOf(staticDataDao.getByType("resident_collection_id").getValue());
        Long cityLordCollectionId = Long.valueOf(staticDataDao.getByType("city_lord_collection_id").getValue());

        //获取空投商品中的城主数量(昨日)
        Integer cityLordAirdropGrantCount = airdropGrantRecordDao.getYesterDayCountByCollectionIdByDay(cityLordCollectionId,day);
        //获取空投商品中的城主数量(昨日)
        Integer residentAirdropGrantCount = airdropGrantRecordDao.getYesterDayCountByCollectionIdByDay(residentCollectionId,day);

        //昨日售卖的城主卡数量
        Integer cityLordOrderCount = orderDao.getYesterDayCountByCollectionIdByDay(cityLordCollectionId,day);
        //昨日售卖居民卡数量
        Integer residentOrderCount = orderDao.getYesterDayCountByCollectionIdByDay(residentCollectionId,day);

        //昨日合成城主数量
        Integer cityLordSynthesisCount = userSynthesisRecordDao.getYesterDayCountByCollectionIdByDay(cityLordCollectionId,day);
        //昨日合成居民数量
        Integer residentLordSynthesisCount = userSynthesisRecordDao.getYesterDayCountByCollectionIdByDay(residentCollectionId,day);

        //昨日新增人数
        Integer userCount = userInfoDao.getYesterDayCountByDay(day);

        ReportUserDayDetails reportUserDayDetails = new ReportUserDayDetails();
        reportUserDayDetails.setNewAddCityLordCount(cityLordAirdropGrantCount + cityLordOrderCount + cityLordSynthesisCount);
        reportUserDayDetails.setNewAddResidentCount(residentAirdropGrantCount + residentOrderCount + residentLordSynthesisCount);
        reportUserDayDetails.setNewAddRtouristsCount(userCount);
        reportUserDayDetails.insert();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        reportUserDayDetails.setCreateTime(sdf.parse(day));
        reportUserDayDetails.updateById();
    }

    @Override
    public ReportUserVO getReporUser() {
        return reportUserDao.getReporUser();
    }

    @Override
    public Page<ReportUserDayDetailsVO> getPageReporUser(String beginTime, String endTime, Integer pageNo, Integer pageSize) {
        return reportUserDao.getPageReporUser(new Page<>(pageNo,pageSize),beginTime,endTime);
    }

    @Override
    public void insertEcologyData() {
        List<ReportSaveDTO> list = new ArrayList<>();
        //用户总投入
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.ECOLOGY_HATCH_TOTAL_USER_INPUT,  Convert.toStr(userEcologyHatchRecordsDao.getSumEcologyPoints()));
        //平台总发放
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.ECOLOGY_HATCH_TOTAL_PLATFORM_DISTRIBUTION,  Convert.toStr(userEcologyExpenditureRecordDao.getSumHatchTotalPlatformDistribution()));
        //铸造消耗总燃料
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.ECOLOGY_HATCH_CAST_TOTAL_FUEL_CONSUMED,  Convert.toStr(userEcologyCastRecordsDao.getSumEcologyHatchCastTotalFuelConsumed()));
        //铸造消耗总燃料
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.ECOLOGY_HATCH_CAST_USER_CONSUMED,  Convert.toStr(userEcologyPointsDao.getSumAmount()));


        //铸造消耗总燃料
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.ECOLOGY_HATCH_YESTER_DAY_EXPEND_POINTS,  Convert.toStr(userEcologyCastRecordsDao.getYesterDaySumEcologyPoints()));
        //昨日发放燃料
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.ECOLOGY_HATCH_YESTER_DAY_ISSUE_POINTS,  Convert.toStr(userEcologyExpenditureRecordDao.getYesterDaySumEcologyPoints()));
        //昨日发放燃料
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.ECOLOGY_CAST_YESTER_DAY_EXPEND_POINTS,  Convert.toStr(userEcologyHatchRecordsDao.getYesterDaySumEcologyPoints()));

        reportOverviewService.saveBath(list);
    }
}